using System.Linq;
using Eisk.BusinessEntities;

namespace Eisk.BusinessLogicLayer
{
    public partial class EmployeeBLL
    {
        partial void OnEmployeeSaving(Employee employee)
        {
            /* the Address line should be unique for all employees */
            Employee otherEmployeeWithSameAddressOfEmployeeToBeSaved = _DatabaseContext.Employees.FirstOrDefault(empObj => empObj.Address == employee.Address && empObj.EmployeeId != employee.EmployeeId);

            if (otherEmployeeWithSameAddressOfEmployeeToBeSaved != null)
                throw new SameAddressException("Error: the Address line should be unique for all employees");

            /* an employee's region and country must be same as his supervisors region and country */
            if (employee.Supervisor != null)
            {
                if (employee.Country != employee.Supervisor.Country)
                    throw new SupervisorCountryNotSameException("Error: an employee's country must be same as his supervisors region and country");

            }

        }

    }
}
